package com.shenglue.ssi.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.shenglue.ssi.domain.User;
import com.shenglue.ssi.service.UserService;

public class UserAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private User user;
	private UserService userService;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserAction() {
	}

	/**
	 * 用户登录方法
	 */
	public String login() throws Exception {
		User u = userService.isUser(user);
		if (u != null) {
			ActionContext.getContext().getSession().put("user", u);
			return SUCCESS;
		} else {
			return INPUT;
		}
	}

	/**
	 * 查询所有用户方法
	 */
	public String queryall() throws Exception {
		List<User> list = userService.selectUser();
		if(list!=null){
			HttpServletRequest request = ServletActionContext.getRequest();
			request.setAttribute("userList",list);
		}else{
			// 不能认为是错误，因为一开始是没有数据的  return INPUT;
		}
		return SUCCESS;
	}

	/**
	 * 用户注册方法
	 */
	public String register() throws Exception {
		boolean flag = userService.saveUser(user);
		if(flag==true){
			return SUCCESS;
		}else{
			return ERROR;
		}
	}

	/**
	 * 删除用户信息方法
	 */
	public String delete() throws Exception {

		//获取request对象
		HttpServletRequest request = ServletActionContext.getRequest();

		String userName=request.getParameter("name");

		boolean flag = userService.deleteUser(userName);
		if(flag==true){
			return SUCCESS;
		}else{
			return ERROR;
		}
	}

	/**
	 * 用户信息修改
	 */
	public String update() throws Exception {

		boolean flag = userService.updateUser(user);
		if(flag==true){
			return SUCCESS;
		}else{
			return ERROR;
		}

	}
}
